package com.banzhi.shop.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.OrderDetailsAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.BuyOrderDetailsBean;
import com.banzhi.shop.entity.OrderDetails.OrderDetails_data;
import com.banzhi.shop.model.CallBack.OrderDetailsCallBack;
import com.banzhi.shop.model.IOrderDetailsModel;
import com.banzhi.shop.model.Impl.BuyOrderDetailsImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.ListViewForScrollView;
import com.google.zxing.common.StringUtils;

import org.json.JSONObject;

//购买订单详情
public class BuyOrderDetailsActivity extends BaseActivity implements OrderDetailsCallBack, View.OnClickListener {

    private String mId;
    private BuyOrderDetailsImpl model;
    /**
     * 传过来的订单ID
     */
    private String id;
    /**
     * 传过来的订单状态
     */
    private String status;
    /**
     * 传过来的状态，判断是自己的订单还是下线的订单
     */
    private String shoporder;
    /**
     * 退出
     */
    private ImageView ivback;
 /*   private IOrderDetailsModel model;*/
    /**
     * 实体类
     */
    private OrderDetails_data orderdata;
    /**
     * 名字
     */
    private TextView tvname;
    /**
     * 电话
     */
    private TextView tvphone;
    /**
     * 地址
     */
    private TextView tvaddress;
    /**
     * 总价
     */
    private TextView tvzongjia;
    /**
     * 快递
     */
    private TextView tvkuaidi;
    /**
     * 优惠
     */
    private TextView tvyouhui;
    /**
     * 订单总价
     */
    private TextView tvdingdanzongjia;
    /**
     * 实际付款
     */
    private TextView tvshijifukuan;
    /**
     * 订单状态
     */
    private ImageView ivstatus;
    /**
     * 订单状态
     */
    private TextView tvstatus;
    /**
     * 支付方式
     */
    private TextView tvzhifu;
    /**
     * 支付时间
     */
    private TextView tvzhifutime;
    /**
     * 商品列表
     */
    private ListViewForScrollView listview;
    /**
     * 订单号
     */
    private TextView tvdingdanhao;
    /**
     * 订单创建时间
     */
    private TextView tvtime;
    private TextView tvfuzhi;
    private OrderDetailsAdapter adapter;
    private LinearLayout llzhifu;
    private TextView tvfuzhiwuliu;
    private LinearLayout llpaytime;
    private LinearLayout llwuliuname;
    private TextView tvwuliuname;
    private LinearLayout llwuliunum;
    private TextView tvwuliunum;
    private TextView tvdianzhulijian;
    private RelativeLayout rllijian;
    private BuyOrderDetailsBean mDetailsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_order_details);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        Intent intent = getIntent();
        mId = intent.getStringExtra("id");

        model = new BuyOrderDetailsImpl();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getOrderData(mId,this);



    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        tvfuzhi.setOnClickListener(this);
        tvfuzhiwuliu.setOnClickListener(this);
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        tvname = (TextView) findViewById(R.id.tv_name);
        tvphone = (TextView) findViewById(R.id.tv_phone);
        tvaddress = (TextView) findViewById(R.id.tv_address);
        tvzongjia = (TextView) findViewById(R.id.tv_zongjia);
        tvkuaidi = (TextView) findViewById(R.id.tv_kuaidi);
        tvyouhui = (TextView) findViewById(R.id.tv_youhui);
        tvzhifu = (TextView) findViewById(R.id.tv_zhifu);
        tvfuzhiwuliu = (TextView) findViewById(R.id.tv_fuzhiwuliu);
        tvdingdanzongjia = (TextView) findViewById(R.id.tv_dingdanzongjia);
        tvshijifukuan = (TextView) findViewById(R.id.tv_shifukuan);
        listview = (ListViewForScrollView) findViewById(R.id.listview);
        ivstatus = (ImageView) findViewById(R.id.iv_status);
        tvstatus = (TextView) findViewById(R.id.tv_status);
        tvdingdanhao = (TextView) findViewById(R.id.tv_bianhao);
        llzhifu = (LinearLayout) findViewById(R.id.ll_zhifu);
        tvtime = (TextView) findViewById(R.id.tv_time);
        tvfuzhi = (TextView) findViewById(R.id.tv_fuzhi);
        tvzhifutime = (TextView) findViewById(R.id.tv_zhifutime);
        llpaytime = (LinearLayout) findViewById(R.id.ll_paytime);
        tvwuliuname = (TextView) findViewById(R.id.tv_wuliuname);
        llwuliuname = (LinearLayout) findViewById(R.id.ll_wuliuname);
        tvwuliunum = (TextView) findViewById(R.id.tv_wuliunumber);
        tvdianzhulijian = (TextView) findViewById(R.id.tv_dianzhulijian);
        llwuliunum = (LinearLayout) findViewById(R.id.ll_wuliunumber);
        rllijian = (RelativeLayout) findViewById(R.id.rl_lijian);

    }

    @Override
    public void ongetDataSuccess(Object object) {
        mDetailsBean = (BuyOrderDetailsBean) object;
        showViews();
    }

    private void showViews() {
        if ("0".equals(mDetailsBean.getStatus())) {
            tvstatus.setText("待付款");
            ivstatus.setImageResource(R.mipmap.daifukuan);
            llzhifu.setVisibility(View.GONE);
        } else if ("1".equals(mDetailsBean.getStatus())) {
            tvstatus.setText("待发货");
            ivstatus.setImageResource(R.mipmap.daifahuo);
        } else if ("2".equals(mDetailsBean.getStatus())) {
            tvstatus.setText("待收货");
            ivstatus.setImageResource(R.mipmap.daishouhuo);
        } else if ("3".equals(mDetailsBean.getStatus())) {
            tvstatus.setText("已完成");
            ivstatus.setImageResource(R.mipmap.chenggong);
        } else {
            tvstatus.setText("已关闭");
            tvstatus.setTextColor(Color.GRAY);
        }
        tvname.setText(mDetailsBean.getAddress().getRealname());
        tvphone.setText(mDetailsBean.getAddress().getMobile());
        tvaddress.setText(mDetailsBean.getAddress().getAddress());
        if (ASUtils.isCanUse(mDetailsBean.getPaytype())) {
            tvzhifu.setText(mDetailsBean.getPaytype());
        }

        tvzongjia.setText(mDetailsBean.getPrice());
        if (ASUtils.isCanUse(mDetailsBean.getFreight())) {
            tvkuaidi.setText(NumberUtils.DecimalFormat(mDetailsBean.getFreight()));
        }
        if (ASUtils.isCanUse(mDetailsBean.getPrice())) {
            tvdingdanzongjia.setText(NumberUtils.DecimalFormat(mDetailsBean.getPrice()));
        }
        if (ASUtils.isCanUse(mDetailsBean.getPrice())) {
            tvshijifukuan.setText(mDetailsBean.getPrice());
        }
        if (ASUtils.isCanUse(mDetailsBean.getPaytime())) {

            tvzhifutime.setText(mDetailsBean.getPaytime());
        }
        if (ASUtils.isCanUse(mDetailsBean.getOrderno())) {
            tvdingdanhao.setText(mDetailsBean.getOrderno());
        }

        if (ASUtils.isCanUse(mDetailsBean.getCreatetime())){
            tvtime.setText(mDetailsBean.getCreatetime());
        }

        if (ASUtils.isCanUse(mDetailsBean.getExpresscom())) {
            llwuliuname.setVisibility(View.GONE);

        } else {
            llwuliuname.setVisibility(View.VISIBLE);
            tvwuliuname.setText(mDetailsBean.getExpresscom());
        }

        if (ASUtils.isCanUse(mDetailsBean.getExpresssn())) {
            llwuliunum.setVisibility(View.GONE);

        } else {
            llwuliunum.setVisibility(View.VISIBLE);
            tvwuliunum.setText(mDetailsBean.getExpresssn());
        }

    }

    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
     //   NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_fuzhi:
                if (ASUtils.isCanUse(mDetailsBean.getExpresscom())) {
                    ShareUtils.copyLianjie(this, mDetailsBean.getExpresscom());
                }

                break;
            case R.id.tv_fuzhiwuliu:
                if (mDetailsBean.getExpresssn()!= null) {
                    ShareUtils.copyLianjie(this, mDetailsBean.getExpresssn());
                }

                break;
        }
    }
}
